package com.mfangsoft.zhuangjialong.integration.activity.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.app.entryactivity.model.BaseEntryActivity;
import com.mfangsoft.zhuangjialong.app.order.mapper.AppOrderMapper;
import com.mfangsoft.zhuangjialong.app.order.model.OrderGift;
import com.mfangsoft.zhuangjialong.app.order.model.OrderInfo;
import com.mfangsoft.zhuangjialong.app.weixin.mapper.WeiXinMapper;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.core.exception.ServiceException;
import com.mfangsoft.zhuangjialong.integration.activity.mapper.BaseActivityInfoMapper;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfo;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfoItem;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityShop;
import com.mfangsoft.zhuangjialong.integration.activity.service.ActivityService;
import com.mfangsoft.zhuangjialong.integration.customer.model.Customer;
import com.mfangsoft.zhuangjialong.integration.gift.model.BaseGift;
import com.mfangsoft.zhuangjialong.integration.order.model.BaseOrder;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.model.BaseStatisticsActivity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.service.StatisticsActivityService;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserRelationEntity;



@Service
public class ActivityServiceimpl implements ActivityService {
	
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ActivityServiceimpl.class); 
	
	
	@Autowired
	private BaseActivityInfoMapper baseActivityInfoMapper;
	
	@Autowired
	private StatisticsActivityService statisticsActivityService; //统计
	
	@Autowired
	private UserEntityMapper userEntityMapper;
	
	@Autowired
	private AppOrderMapper appOrderMapper;
	@Autowired
	WeiXinMapper weiXinMapper;
	
	
	


	@Override
	public void addActivbity(BaseActivityInfo baseActivityInfo) throws ServiceException {
			
	  baseActivityInfoMapper.addActivbity(baseActivityInfo);
		
	List<BaseActivityInfoItem> items =baseActivityInfo.getActivityInfoItems();
	  if(items != null){
		  for (BaseActivityInfoItem baseActivityInfoItem : items) {
			  
			  baseActivityInfoItem.setActivity_id(baseActivityInfo.getId());
			  
			  baseActivityInfoMapper.addActivbityItem(baseActivityInfoItem);//活动内容增加
			
		  	}
	  }
	  List<BaseActivityShop> shopitems	=baseActivityInfo.getBaseActivityShop();
	  
	  if(shopitems != null){
		  
		  for (BaseActivityShop baseActivityShop : shopitems) {
			  baseActivityShop.setActivity_id(baseActivityInfo.getId());
			  baseActivityInfoMapper.addActivbityShop(baseActivityShop);
			  
		  }
	  }
		
	}


	@Override
	public void addActivbityItem(BaseActivityInfoItem baseActivityInfoItem) {
		baseActivityInfoMapper.addActivbityItem(baseActivityInfoItem);
		
	}


	@Override
	public Page<BaseActivityInfo> selectActivity(Page<BaseActivityInfo> page) {
		UserContext.getCurrentUser().getUser_level();
		UserContext.getCurrentUser().getId();
		UserRelationEntity userRelation = new UserRelationEntity();
		userRelation.setId(UserContext.getCurrentUser().getId());
		userRelation = baseActivityInfoMapper.userRelation(userRelation);
		
		Boolean  isTop = UserContext.getCurrentUser().getId().equals("153")?true:false;

		Map<String,Object> mp=(Map<String, Object>) page.getParam();
		
		
		
		if(UserContext.getCurrentUser().getUser_level()==1){
			
			mp.put("user_id", UserContext.getCurrentUser().getId());
			page.setUser_id(UserContext.getCurrentUser().getId());
			page.setParam(mp);
			page.setData(baseActivityInfoMapper.selectListActivityPage(page));
			
		}else if(UserContext.getCurrentUser().getId() == 153){
			 page.setUser_id(UserContext.getCurrentUser().getId());
			 mp.put("user_id", UserContext.getCurrentUser().getId());
			 page.setParam(mp);
			 page.setData(baseActivityInfoMapper.selectListActivityAdminPage(page));
			
		}else if(UserContext.getCurrentUser().getUser_level()==3){
			
			 mp.put("create", UserContext.getCurrentUser().getId());
			/* mp.put("user_id", UserContext.getCurrentUser().getId());*/
			/* mp.put("state",1);*/
			 page.setUser_id(UserContext.getCurrentUser().getId());
			 page.setParam(mp);
			 page.setData(baseActivityInfoMapper.selectListActivityPage(page));
		}
		
		
		
		page.setIsTop_level(isTop);
		
		 
		 
		 
		 return page;
	}
	@Override
	public Page<BaseActivityInfo> selectAdminActivityPage(Page<BaseActivityInfo> page) {
		UserContext.getCurrentUser().getUser_level();
		UserContext.getCurrentUser().getId();
		UserRelationEntity userRelation = new UserRelationEntity();
		userRelation.setId(UserContext.getCurrentUser().getId());
		userRelation = baseActivityInfoMapper.userRelation(userRelation);
		
		Boolean  isTop = UserContext.getCurrentUser().getId().equals("153")?true:false;

		Map<String,Object> mp=(Map<String, Object>) page.getParam();
		
		if(UserContext.getCurrentUser().getUser_level()==1){
			
			
			
		}else if(UserContext.getCurrentUser().getId() == 153){
			 page.setUser_id(UserContext.getCurrentUser().getId());
			 mp.put("user_id", UserContext.getCurrentUser().getId());
			 page.setParam(mp);
			 page.setData(baseActivityInfoMapper.selectAdminActivityPage(page));
			
		}else if(UserContext.getCurrentUser().getUser_level()==3){
			
			 mp.put("create", UserContext.getCurrentUser().getId());
			/* mp.put("user_id", UserContext.getCurrentUser().getId());*/
			/* mp.put("state",1);*/
			 page.setUser_id(UserContext.getCurrentUser().getId());
			 page.setParam(mp);
			 page.setData(baseActivityInfoMapper.selectAdminActivityPage(page));
		}else if(UserContext.getCurrentUser().getUser_level()==2){
			
			 mp.put("create", UserContext.getCurrentUser().getId());
			/* mp.put("user_id", UserContext.getCurrentUser().getId());*/
			/* mp.put("state",1);*/
			 page.setUser_id(UserContext.getCurrentUser().getId());
			 page.setParam(mp);
			 page.setData(baseActivityInfoMapper.selectAdminActivityPage(page));
		}
		
		
		page.setIsTop_level(isTop);
		
		 
		 
	
		
		return page;
	}


	@Override
	public void updateActivity(BaseActivityInfo itme) {

			baseActivityInfoMapper.updateActivityOne(itme);

	}


	@Override
	public void updateActivityPutaway(BaseActivityInfo baseActivityInfo) {
		baseActivityInfoMapper.updateActivityPutaway(baseActivityInfo);
	}


	@Override
	public Map<String, Object> selectActivityOne(Long id) {
		Map<String, Object> map = new HashMap<>();
		
		Map<String, Object> baseActivityInfo = baseActivityInfoMapper.selectActivityOne(id);
		List<BaseActivityInfoItem> list0 = baseActivityInfoMapper.selectActivityOneItme(id);
		
		
		BaseEntryActivity baseEntryActivity = new BaseEntryActivity();
		baseEntryActivity.setActivity_id(id.intValue());
		
		 Map<String, Object>  fore = baseActivityInfoMapper.selectActivityItmesGiftF(baseEntryActivity);
		 if(fore!=null){
		 String gifts =  (String) fore.get("gift");
		 if(gifts!=null){
		 String[] g = gifts.split(",");
		 List list = Arrays.asList(g);
		 List<Map<String, Object>>  foreGift	=	baseActivityInfoMapper.selectActivityItmesGiftFG(list);
		 map.put("egggift", foreGift);
		 }
		 }
		 Map<String, Object>  five = baseActivityInfoMapper.selectActivityItmesGiftFive(baseEntryActivity);
		 if(five!=null){
		 String giftf =  (String) five.get("giftf");
		 if(giftf!=null){
		 String[] fg = giftf.split(",");
		 List list2 = Arrays.asList(fg);
		 List<Map<String, Object>>  fiveGift	=	baseActivityInfoMapper.selectActivityItmesGiftFiveG(list2);
		 map.put("ticketgift", fiveGift);
		 }
		 }
		 Map<String, Object>  one = baseActivityInfoMapper.selectActivityItmesGiftone(baseEntryActivity);
		 if(one!=null){
		 String gifto =  (String) one.get("gifto");
		 if(gifto!=null){
		 String[] og = gifto.split(",");
		 List list3 = Arrays.asList(og);
		 List<Map<String, Object>>  fiveOne	=	baseActivityInfoMapper.selectActivityItmesGiftOneG(list3);
		 map.put("sigogift", fiveOne);
		 }
		 }
		 Map<String, Object>  six = baseActivityInfoMapper.selectActivityItmesGiftSix(baseEntryActivity);
		 if(six!=null){
		 String giftsix =  (String) six.get("giftsix");
		 if(giftsix!=null){
		 String[] oggiftsix = giftsix.split(",");
		 List list6 = Arrays.asList(oggiftsix);
		 List<Map<String, Object>>  sixGift	=	baseActivityInfoMapper.selectActivityItmesGiftSixG(list6);
		 map.put("sixGift", sixGift);
		 }
		 }
		 Map<String, Object>  seven = baseActivityInfoMapper.selectActivityItmesGiftSeven(baseEntryActivity);
		 if(seven!=null){
		 String giftseven =  (String) seven.get("giftseven");
		 if(giftseven!=null){
		 String[] oggiftseven = giftseven.split(",");
		 List list7 = Arrays.asList(oggiftseven);
		 List<Map<String, Object>>  sevenGift	=	baseActivityInfoMapper.selectActivityItmesGiftSevenG(list7);
		 map.put("sevenGift", sevenGift);
		 }
		 }else{
			 map.put("sevenGift", null);
		 }
		 
		 Map<String, Object>  eight = baseActivityInfoMapper.selectActivityItmesGiftEight(baseEntryActivity);
		 if(eight!=null){
		 String gifteight =  (String) eight.get("gifteight");
		 if(gifteight!=null){
		 String[] oggifteight = gifteight.split(",");
		 List list8 = Arrays.asList(oggifteight);
		 List<Map<String, Object>>  eightGift	=	baseActivityInfoMapper.selectActivityItmesGiftEightG(list8);
		 map.put("eightGift", eightGift);
		 }
		 }else{
			 map.put("eightGift", null);
		 }
		  
		
		
		int bid = id.intValue(); 
		List<Map<String, Object>> map2 =  baseActivityInfoMapper.shelectShopList(bid);
		
		map.put("baseActivityInfo", baseActivityInfo);
		map.put("itmes", list0);
		map.put("shops", map2);
				
		return map;
	}


	@Override
	public void updateActivityOne(BaseActivityInfo baseActivityInfo) {
			  baseActivityInfoMapper.updateActivityOne(baseActivityInfo);
			  
			  int did = baseActivityInfo.getId();
			  baseActivityInfoMapper.delectActivityShop(did);
			  baseActivityInfoMapper.delectActivityItems(did);
			  
			  List<BaseActivityShop> shopitems	=baseActivityInfo.getBaseActivityShop();//参与店铺
			  List<BaseActivityInfoItem> items	=baseActivityInfo.getActivityInfoItems();//活动内容
			  
			if(shopitems != null){
				  
				  for (BaseActivityShop baseActivityShop : shopitems) {
					  baseActivityShop.setActivity_id(baseActivityInfo.getId());
					  baseActivityInfoMapper.addActivbityShop(baseActivityShop);
					  
				  }
			  }
			  	if(items != null){
				  
				  for (BaseActivityInfoItem baseActivityInfoItem :items) {
					  baseActivityInfoItem.setActivity_id(baseActivityInfo.getId());
					  baseActivityInfoMapper.addActivbityItems(baseActivityInfoItem);
					  
				  }
			  }
			  
			  
	}


	@Override
	public List<BaseActivityInfo> selectActivityItem(BaseActivityInfo baseActivityInfo) {
		return baseActivityInfoMapper.selectActivityItem(baseActivityInfo);
	}


	@Override
	public void addActivbityShop(BaseActivityShop num) {
		baseActivityInfoMapper.addActivbityShop(num);
	}


	@Override
	public Map<String, Object> selectActivityUser(BaseEntryActivity baseEntryActivity) {
		Map<String, Object> map = baseActivityInfoMapper.selectActivityUser(baseEntryActivity);
		Map<String, Object> map2 = baseActivityInfoMapper.selectActivityUserItmes(baseEntryActivity);//抽奖券5
		Map<String, Object> map3 =baseActivityInfoMapper.selectActivityUserItmesTwo(baseEntryActivity);//金蛋4
		Map<String, Object> map4 =baseActivityInfoMapper.selectActivityUserItmesThree(baseEntryActivity);//礼品6-20
		List<Map<String, Object>> map5 =baseActivityInfoMapper.selectActivityUserItmesFor(baseEntryActivity);
		map.put("choujiang", map2);//抽奖
		map.put("jindan", map3);//金蛋
		map.put("lipin", map4);//礼品
		map.put("dingdan", map5);//订单
		return map;
	}


	@Override
	public Map<String, Object> selectHelperActivityUser(BaseEntryActivity baseEntryActivity) {
//		Map<String,Object> m=new HashMap<>();
//		m.put("customerId", baseEntryActivity.getCustomer_id());
//		m.put("shopId", baseEntryActivity.getShop_id());
//		Integer dangQianShopSellerId = weiXinMapper.selectSeller(m);
//		Map<String, Object> seller = appOrderMapper.selectSellerByCustomerId(m);
//		if(dangQianShopSellerId==null){
//			if(seller!=null){
//				Map<String,Object> orderInfo=new HashMap<>();
//				orderInfo.put("shopId", baseEntryActivity.getShop_id());
//				orderInfo.put("sellerId", baseEntryActivity.getSellerId());
//				orderInfo.put("customerId", baseEntryActivity.getCustomer_id());
//				appOrderMapper.updateCustomerSeller(orderInfo);
//			}else{
//				OrderInfo order=new OrderInfo();
//				order.setCustomerId(baseEntryActivity.getCustomer_id());
//				order.setSellerId(baseEntryActivity.getSellerId());
//				order.setShopId(baseEntryActivity.getShop_id());
//				appOrderMapper.addCustomerSeller(order);
//			}
//		}
		Map<String, Object> map = new HashMap();
		Map<String, Object> map2 = new HashMap();
		
		
		
		 map = baseActivityInfoMapper.selectHelperActivityUser(baseEntryActivity);
		 
		 Double deposit_price = baseActivityInfoMapper.selectHelperActivityUserPrice(baseEntryActivity);
		 if(deposit_price != null){
			 map.put("deposit_price", deposit_price);
		 }
		
		 map2 = baseActivityInfoMapper.selectActivityCard(baseEntryActivity);
		
		 List<Map<String, Object>> map3  = baseActivityInfoMapper.selectActivityItmes(baseEntryActivity);
		 if(map2 != null){
		 map.put("isdefault", map2.get("isdefault"));
		 }
		 map.put("Itmes", map3);
		 
		 Map<String, Object>  fore = baseActivityInfoMapper.selectActivityItmesGiftF(baseEntryActivity);
		 if(fore!=null){
		 String gifts =  (String) fore.get("gift");
		 if(gifts!=null){
		 String[] g = gifts.split(",");
		 List list = Arrays.asList(g);
		 List<Map<String, Object>>  foreGift	=	baseActivityInfoMapper.selectActivityItmesGiftFG(list);
		 map.put("egggift", foreGift);
		 }
		 }
		 Map<String, Object>  five = baseActivityInfoMapper.selectActivityItmesGiftFive(baseEntryActivity);
		 if(five!=null){
		 String giftf =  (String) five.get("giftf");
		 if(giftf!=null){
		 String[] fg = giftf.split(",");
		 List list2 = Arrays.asList(fg);
		 List<Map<String, Object>>  fiveGift	=	baseActivityInfoMapper.selectActivityItmesGiftFiveG(list2);
		 map.put("ticketgift", fiveGift);
		 }
		 }
		 
		 Map<String, Object>  one = baseActivityInfoMapper.selectActivityItmesGiftone(baseEntryActivity);
		 if(one!=null){
		 String gifto =  (String) one.get("gifto");
		 if(gifto!=null){
		 String[] og = gifto.split(",");
		 List list3 = Arrays.asList(og);
		 List<Map<String, Object>>  fiveOne	=	baseActivityInfoMapper.selectActivityItmesGiftOneG(list3);
		 map.put("sigogift", fiveOne);
		 }
		 
		 }
		return map;
	}


	@Override
	public void uptdateSign(BaseEntryActivity baseEntryActivity) {
		 baseActivityInfoMapper.uptdateSign(baseEntryActivity);
	}


	@Override
	public void uptdateCard(BaseEntryActivity baseEntryActivity) {
		 baseActivityInfoMapper.uptdateSign(baseEntryActivity);
	}


	@Override
	public BaseActivityInfo selectSignGo(BaseActivityInfo baseActivityInfo) {
		return baseActivityInfoMapper.selectSignGo(baseActivityInfo);
	}


	@Override
	public Page<Map<String, Object>> selectHelperActivityPage(Page<Map<String, Object>> page) {
		
		// page.setUser_id(UserContext.getCurrentUser().getId());
		 List<Map<String, Object>> map = baseActivityInfoMapper.selectHelperActivityPage(page);
		/* Map<String, Object> total = baseActivityInfoMapper.selectHelperActivityPageTotal(page);
		 map.add(total);*/
		
		 page.setData(map);
		 return page;
	}


	@Override
	public Map<String, Object> selectHelperActivityAll(BaseActivityInfo baseActivityInfo) {
		
		 BaseEntryActivity baseEntryActivity = new BaseEntryActivity();
		 baseEntryActivity.setActivity_id(baseActivityInfo.getId());
		 baseEntryActivity.setCustomer_id(baseActivityInfo.getCustomer_id());
		 Map<String, Object> map =  baseActivityInfoMapper.selectHelperActivityAll(baseActivityInfo);
		 if(map!=null){
		 int num = baseActivityInfoMapper.selectHelperActivityAllNum(baseActivityInfo);//报名人数 
		 List<Map<String, Object>>  map2 =  baseActivityInfoMapper.selectHelperActivityAllItmes(baseActivityInfo);
		 List<Map<String, Object>>  map3 =  baseActivityInfoMapper.selectHelperActivityAllPicture(baseActivityInfo);//报名头像
		 
		/*if(map2.size()!=0){
			if(map2.size()>=1){
		 String a= "活动卡：购买金额"+map2.get(0).get("buy_money")+"元抵"+map2.get(0).get("mortgage_money")+"元,满"+map2.get(0).get("use_money")+"可使用";
		 map.put("a", a);
			}
			if(map2.size()>=2){
		 String b= "签到礼："+map2.get(1).get("name");
		 map.put("b", b);
			}
			if(map2.size()>=3){
		 String c= "预付金额："+map2.get(2).get("use_money")+"元";
		 map.put("c", c);
			}
			if(map2.size()>=4){
		 String d= "砸金蛋：消费满"+map2.get(3).get("use_money")+"元,可以获得一次砸金蛋机会";
		 map.put("d", d);
			}
			if(map2.size()>=5){
		 String e= "抽奖券：消费满"+map2.get(4).get("use_money")+"元,可获得一张抽奖券";
		 	map.put("e", e);
			}
			if(map2.size()>=6){
		 String f= "礼	品：消费满"+map2.get(5).get("use_money")+"可获得"+map2.get(5).get("name")+"一份";
		 map.put("f", f);
			}
		}*/
		 for (Map<String, Object> map4 : map2) {
			
			 String v= (String) map4.get("view_msg");
			 
		     Iterator<Map.Entry<String, Object>> it = map4.entrySet().iterator();
		        while (it.hasNext()) {
		          Map.Entry<String, Object> entry = it.next();
		            
		          if(entry.getValue()!=null){
		        	 
		        	 v=v.replace(entry.getKey(), entry.getValue().toString()); 
		          }         
		       }
		        
		   map4.put("view_msg", v);  
		}
		 
		 
		 
		 map.put("num", num);
		 map.put("itmes", map2);
		 map.put("pictures", map3);
		
		 
		 int lookNum =  (int) map.get("look");
		 
		 baseActivityInfo.setLook(lookNum+1);
		 
		 baseActivityInfoMapper.uptdateHeilernum(baseActivityInfo);
		 
		 }
		return map;
	}


	
	
	@Override
	public Page<BaseActivityInfo> selectActivityAuditPage(Page<BaseActivityInfo> page) {
		
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			Map<String,Object> map=(Map<String, Object>) page.getParam();
			if(map!=null){
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}else{
				map=new HashMap<String, Object>();
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}
			page.setParam(map);
			page.setData(baseActivityInfoMapper.selectActivityAuditPage(page));
			return page;
		}
		return null;
		/*UserContext.getCurrentUser().getUser_level();
		UserContext.getCurrentUser().getId();
		UserRelationEntity userRelation = new UserRelationEntity();
		userRelation.setId(UserContext.getCurrentUser().getId());
		userRelation = baseActivityInfoMapper.userRelation(userRelation);
		
		Boolean  isTop = UserContext.getCurrentUser().getUserInfoEntity().getCreater().equals("153")?true:false;
		
		Map<String,Object> mp=(Map<String, Object>) page.getParam();
		
		
			
			mp.put("create", UserContext.getCurrentUser().getId());
			
			
			 page.setUser_id(UserContext.getCurrentUser().getId());
		
		page.setData(baseActivityInfoMapper.selectActivityAuditPage(page));
		
		return page;*/
	}


	@Override
	public BaseEntryActivity selectHelperActivityManage(BaseEntryActivity baseEntryActivity) {
		
		return baseActivityInfoMapper.selectHelperActivityManage(baseEntryActivity);
	}


	@Override
	public BaseOrder selectHelperAllActivityOrder(BaseOrder baseOrder) {
		
		return appOrderMapper.selectHelperAllActivityOrder(baseOrder);
	}


	@Override
	public Page<Map<String, Object>> selectActivityUsers(Page<Map<String, Object>> page) {
		
		
	   Map<String,Object>	mp=(Map<String, Object>) page.getParam();
	   

	   if(UserContext.getCurrentUser().getId() == 153){
		   
		    
			   List<Map<String, Object>> map = baseActivityInfoMapper.selectActivityUsersPage(page);
				Map pageMpa =  (Map) page.getParam();
				
				for(Map<String, Object> newmap : map){
					Map<String, Object> dateMap = new HashMap<>(); 
				
					int customer_id = Integer.valueOf(newmap.get("customer_id").toString());
					dateMap.put("activity_id", pageMpa.get("id"));
					dateMap.put("customer_id", customer_id);
					Map<String, Object> resultMap =baseActivityInfoMapper.selectActivityEggs(dateMap);
					if(resultMap!=null){
					boolean flag=resultMap.containsKey("eggs");
					if(flag==true){
					newmap.put("eggs", resultMap.get("eggs"));
					}
					newmap.put("egghsnum", resultMap.get("egghsnum"));
					newmap.put("tickets", resultMap.get("tickets"));
					newmap.put("tickethsnum", resultMap.get("tickethsnum"));
					
					newmap.put("egggift", resultMap.get("egggift"));
					newmap.put("ticketgift", resultMap.get("ticketgift"));
					newmap.put("real_pay_price", resultMap.get("real_pay_price"));
					}
				}
				
				page.setData(map);
				return page;
		   
	   }
	   
	   
	   if(UserContext.getCurrentUser().getUser_level().intValue()!=1){
		   
		   
		   mp.put("create", UserContext.getCurrentUser().getId());
		   
	   }else{
		   
		   mp.put("shop_id", UserContext.getCurrentUser().getId());
	   }
		
		List<Map<String, Object>> map = baseActivityInfoMapper.selectActivityUsersPage(page);
		Map pageMpa =  (Map) page.getParam();
		
		for(Map<String, Object> newmap : map){
			Map<String, Object> dateMap = new HashMap<>(); 
		
			int customer_id = Integer.valueOf(newmap.get("customer_id").toString());
			dateMap.put("activity_id", pageMpa.get("id"));
			dateMap.put("customer_id", customer_id);
			Map<String, Object> resultMap =baseActivityInfoMapper.selectActivityEggs(dateMap);
			if(resultMap!=null){
			boolean flag=resultMap.containsKey("eggs");
			if(flag==true){
			newmap.put("eggs", resultMap.get("eggs"));
			}
			newmap.put("egghsnum", resultMap.get("egghsnum"));
			newmap.put("tickets", resultMap.get("tickets"));
			newmap.put("tickethsnum", resultMap.get("tickethsnum"));
			
			newmap.put("egggift", resultMap.get("egggift"));
			newmap.put("ticketgift", resultMap.get("ticketgift"));
			newmap.put("real_pay_price", resultMap.get("real_pay_price"));
			}
		}
		
		
		
		page.setData(map);
		return page;
	}


	@Override
	public void uptdateHelperActivityOrder(OrderGift orderGift) {
		if(orderGift.getStatus().equals("1")){
		 baseActivityInfoMapper.uptdateHelperActivityOrder(orderGift);
		 BaseGift baseGift  = new BaseGift();
			
			baseGift.setId(orderGift.getGiftId());
			baseGift.setGift_id(orderGift.getGiftId());
			baseActivityInfoMapper.uptdateGiftnum(baseGift);
			
			Map map = new HashMap<>();
			map.put("activityId", orderGift.getActivityId());
			
			baseActivityInfoMapper.uptdateActivitynum(map);
		}
			if(orderGift.getStatus().equals("3")){
			baseActivityInfoMapper.uptdateHelperActivityOrder(orderGift);
			BaseStatisticsActivity statistics =new BaseStatisticsActivity();
			statistics.setActivity_id(orderGift.getActivityId());
			statistics.setUsenum(1);
			statisticsActivityService.updateStatistics(statistics);
			}
	}


	@Override
	public Map<String, Object> selectXCXActivity(BaseActivityInfo baseActivityInfo) {


			 BaseEntryActivity baseEntryActivity = new BaseEntryActivity();
			 baseEntryActivity.setActivity_id(baseActivityInfo.getId());
			 baseEntryActivity.setCustomer_id(baseActivityInfo.getCustomer_id());
			 Map<String, Object>  map  = baseActivityInfoMapper.selectActivityItmesXCX(baseEntryActivity); //基本信息
			 List<Map<String, Object>> map3  = baseActivityInfoMapper.selectActivityItmes(baseEntryActivity);//奖券信息
			 map.put("item", map3);
			 List<Map<String, Object>> map4  = baseActivityInfoMapper.selectActivityComment(baseEntryActivity);
			 map.put("comment", map4);
			return map;
		}


	@Override
	public Map<String, Object> selectXCXSigo(Customer customer) {
		return baseActivityInfoMapper.selectXCXSigo(customer);
	}


	@Override
	public List<Map<String, Object>> selectXCXHistoryActivity(Integer shop_id) {
		return baseActivityInfoMapper.selectXCXHistoryActivity(shop_id);
	}


	@Override
	public List<Map<String, Object>> selectXCXHistoryActivityShop(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return baseActivityInfoMapper.selectXCXHistoryActivityShop(map);
	}



	@Override
	public List<Map<String, Object>> selectXCXHistoryActivityID(Integer shop_id) {
		
		List<Map<String, Object>> list =	 baseActivityInfoMapper.selectXCXHistoryActivityID(shop_id);
		if(list.equals("null")||list.size()<1||list==null){
								list = baseActivityInfoMapper.selectXCXHistoryActivityIDGeneralize(shop_id);
		}
		return  list;
	}



	@Override
	public Map<String, Object> selectMinShopByLBS(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return baseActivityInfoMapper.selectMinShopByLBS(map);
	}


	@Override
	public Map<String, Object> selectHelperActivityDetails(BaseActivityInfo baseActivityInfo) {
		
		 BaseEntryActivity baseEntryActivity = new BaseEntryActivity();
		 baseEntryActivity.setActivity_id(baseActivityInfo.getId());
		 baseEntryActivity.setCustomer_id(baseActivityInfo.getCustomer_id());
		 Map<String, Object>  map  = baseActivityInfoMapper.selectActivityItmesXCX(baseEntryActivity); //基本信息
		 List<Map<String, Object>> map3  = baseActivityInfoMapper.selectActivityItmes(baseEntryActivity);//奖券信息
		 map.put("item", map3);
		 List<Map<String, Object>> map4  = baseActivityInfoMapper.selectHelperActivityDetails(baseEntryActivity);//订单
		 map.put("order", map4);
		return map;
	}


	@Override
	public Page<Map<String, Object>> selectHelperHistoryActivityPage(Page<Map<String, Object>> page) {
		page.setData(baseActivityInfoMapper.selectHelperHistoryActivityPage(page));
		return page;
	}


	@Override
	public Map<String, Object> selectSigoName(BaseActivityInfo baseActivityInfo) {
		BaseEntryActivity baseEntryActivity = new BaseEntryActivity();
		 baseEntryActivity.setActivity_id(baseActivityInfo.getId());
		 baseEntryActivity.setCustomer_id(baseActivityInfo.getCustomer_id());
		 baseEntryActivity.setShop_id(baseActivityInfo.getShop_id());
		 int sigoname = baseActivityInfoMapper.selectSigoName(baseEntryActivity);
		 
		 Map<String, Object> map =  new HashMap<>();
				 
		 map.put("sigoname",sigoname);
		 
		return map;
	}


	@Override
	public Integer selectActivityCode(String code) {
		int aCode = baseActivityInfoMapper.selectActivityCode(code);
		return aCode;
	}


	@Override
	public void addAdminActivity(BaseActivityInfo baseActivityInfo) throws ServiceException {
		  baseActivityInfoMapper.addAdminActivity(baseActivityInfo);
		  List<BaseActivityShop> shopitems	=baseActivityInfo.getBaseActivityShop();
		  
		  if(shopitems != null){
			  
			  for (BaseActivityShop baseActivityShop : shopitems) {
				  baseActivityShop.setActivity_id(baseActivityInfo.getId());
				  baseActivityInfoMapper.addActivbityShop(baseActivityShop);
				  
			  }
		  }
	}


	@Override
	public List<Map<String, Object>> selectXCXHistoryActivityIDTwo(Integer customer_id) {
		Integer filiale_id = baseActivityInfoMapper.selectFilialeId(customer_id);
	
			 if(filiale_id ==null){
				  filiale_id = 0;
			 }
		 List<Map<String, Object>> list =  baseActivityInfoMapper.selectXCXHistoryActivityIDTwo(filiale_id);
		 return list;
	}


	@Override
	public List<Map<String, Object>> selectActivityIssueList(Integer activity_id) {
		
		return baseActivityInfoMapper.selectActivityIssueList(activity_id);
	}



	




}
